/*
 * @Author: wangjh wangjiahui0710@163.com
 * @Date: 2024-06-11 11:33:52
 * @LastEditors: wangjh wangjiahui0710@163.com
 * @LastEditTime: 2024-07-18 16:42:24
 * @FilePath: \cip-admin\src\pages\park\dual-prevention\investigation-standards\state.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import type { WhManagerBsTbRiskHazardsStdDetailGetPageUsingPost } from "@/genapi/wh";
import { whManagerBsTbRiskHazardsStdDetailGetPageUsingPost } from "@/genapi/wh";
import { usePage } from "@/hooks/use-page";

import type DetailDialog from "./components/detail-dialog.vue";

import type createNodeDialog from "./components/create-node-dialog.vue";

import bus from "vue3-eventbus";

export const [createState, injectState] = createInjectionState(
	() => {
		// 查询条件
		const filter =
			ref<WhManagerBsTbRiskHazardsStdDetailGetPageUsingPost.Body["query"]>();

		const page = usePage({
			initPageSize: 15,
			fetch: async ({ pageSize, currentPage }) => {
				const { data } =
					await whManagerBsTbRiskHazardsStdDetailGetPageUsingPost({
						data: {
							size: pageSize,
							current: currentPage,
							query: { ...filter.value },
						},
					});
				bus.emit("updateTableData", data?.records);
                bus.emit('getActiveNode',filter.value?.stdId)
				return {
					records: data?.records ?? [],
					total: data?.total ?? 0,
					pageSize,
				};
			},
		});

		watchDeep(filter, () => {
			page.execute({ currentPage: 0 });
		});

		// 当前选中的列表项
		const selectedList = shallowRef<typeof page.records.value>([]);

		// 表格组件
		const tableRef =
			shallowRef<InstanceType<GlobalComponents["TableWidget"]>>();

		// 详情弹窗组件
		const detailDialogRef = shallowRef<InstanceType<typeof DetailDialog>>();

		// 创建树节点弹窗组件
		const createNodeDialogRef =
			shallowRef<InstanceType<typeof createNodeDialog>>();

		return {
			page,
			filter,
			selectedList,
			tableRef,
			detailDialogRef,
			createNodeDialogRef,
		};
	},
	{
		injectionKey: "pageState",
	},
);
